import useUserStore from "@/store/user";
import type { Directive, DirectiveBinding } from "vue";
import { isArray } from "lodash-es";
const permission: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const permissionList: Array<string> = useUserStore().permission;
    let hasPermission: boolean = false;
    const value = binding.value;
    if (typeof value === "string") {
      hasPermission = permissionList.includes(value);
    } else if (isArray(value)) {
      hasPermission = permissionList.some((item) => value.includes(item));
    }
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  },
};

export default permission;
